import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Main {
    public static void main(String[] args) {
        // 测试Mysql数据库
        testMysqlDatabase();
        
    }
    private static void testMysqlDatabase() {
        System.out.println("===== Mysql 数据库测试开始 =====");
        Connection connection = null;
        Statement sql = null;
        ResultSet rs = null;
        try {
            // 1. 初始化Mysql连接
            MysqlFactory factory = new MysqlFactory();
            connection = factory.getConnection(
                "com.mysql.cj.jdbc.Driver",
                "jdbc:mysql://localhost:3306/st",
                "root",
                "123456"
            );
    StudentTableOperator studentoperator=factory.createStudentOper(connection);
    System.out.println("===== Mysql 数据库测试成功 =====");
    System.out.println("===== 对Student表操作 =====");
    AddStudentOperations(studentoperator);
    PrintStudent(connection,sql,rs);
    UpdateStudentOperations(studentoperator);
    PrintStudent(connection,sql,rs);
    SelectStudentOperations(studentoperator);
    PrintStudent(connection,sql,rs);
    DeleteStudentOperations(studentoperator);
    PrintStudent(connection,sql,rs);
    System.out.println("===== 对Course表操作 =====");
   CourseTableOperator courseoperator=factory.createCourseOper(connection);
    AddCourseOperations(courseoperator);
    PrintCourse(connection, sql, rs);
    UpdateCourseOperations(courseoperator);
    PrintCourse(connection, sql, rs);
    SelectCourseOperations(courseoperator);
    PrintCourse(connection, sql, rs);
    DeleteCourseOperations(courseoperator);
    PrintCourse(connection, sql, rs);

} catch (Exception e) {
    System.err.println("Mysql测试失败: " + e.getMessage());
    e.printStackTrace();
} 
finally {
    closeConnection(connection);
}
}
private static void SelectCourseOperations(CourseTableOperator courseoperator) throws SQLException {
       courseoperator.selectCourse(6);
    }
private static void DeleteCourseOperations(CourseTableOperator courseoperator) throws SQLException {
     courseoperator.deleteCourse(6);
    }
private static void UpdateCourseOperations(CourseTableOperator courseoperator) throws SQLException {
       courseoperator.updateCourse(6, "Web程序开发", "计算机进阶", 2.9, "2022-2023");
    }
private static void AddCourseOperations(CourseTableOperator courseoperator) throws SQLException {
      courseoperator.insertCourse(6," Web程序开发", "计算机应用", 2.9, "2022-2023");
    }
static void PrintStudent(Connection connection,Statement sql, ResultSet rs) throws SQLException{
    sql=connection.createStatement();
    rs=sql.executeQuery("select * from student");
    while (rs.next()) {
        int id=rs.getInt(1);
        String name=rs.getString(2);
        String gender=rs.getString(3);
        String birthday=rs.getString(4);
        String adress=rs.getString(5);
        String phone=rs.getString(6);
        String student="[id=" + id + ", name=" + name + ", gender=" + gender + ", birthday=" + birthday + ", adress=" + adress
                    + ", phone=" + phone + ", adress=" + adress + "]";
        System.out.printf("%s\n",student);
    }
}
private static void closeConnection(Connection conn) {
    if (conn != null) {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
private static void SelectStudentOperations(StudentTableOperator studentoperator) throws SQLException {
    System.out.println("\n--- 查询操作 ---");
    studentoperator.selectStudent(5220204);
}
private  static void  AddStudentOperations(StudentTableOperator studentoperator) throws SQLException {
    System.out.println("\n--- 添加操作 ---");
    studentoperator.insertStudent(5220204,"张三","女","2002.9.18","安徽蚌埠","18902839473");
    
}
private  static void  UpdateStudentOperations(StudentTableOperator studentoperator) throws SQLException {
    System.out.println("\n--- 更新操作 ---");
    studentoperator.updateStudent(5220204,"张小红","女","2002.9.18","安徽蚌埠","18902839473");
}
private  static void  DeleteStudentOperations(StudentTableOperator studentoperator) throws SQLException {
    System.out.println("\n--- 删除操作 ---");
    studentoperator.deleteStudent(5220204);
}
static void PrintCourse(Connection connection,Statement sql, ResultSet rs) throws SQLException{
    sql=connection.createStatement();
    rs=sql.executeQuery("select * from course");
    while (rs.next()) {
        int id=rs.getInt(1);
        String name=rs.getString(2);
        String Type=rs.getString(3);
        String credit=rs.getString(4);
        String semester=rs.getString(5);
        String Course="[id=" + id + ", name=" + name + ", Type=" + Type + ", credit=" + credit + ", semester=" + semester +  "]";
        System.out.printf("%s\n",Course);
}
}

}
 